package com.superquan.selfstudybootmybatis.controller;

import com.superquan.selfstudybootmybatis.entity.user.User;
import com.superquan.selfstudybootmybatis.service.UserService;
import com.superquan.selfstudybootmybatis.utils.httpapply.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 根据id获取用户信息
     * @param id
     * @return
     */
    @GetMapping(value = "/getUserInfo/{id}")
    public String getUserInfo(@PathVariable(value = "id")int id){
        return userService.getUserInfo(id).toString();
    }

    /**
     * 获取所有的用户信息
     * @return
     */
    @RequestMapping(value = "/selectAll")
    public List<User> selectAllUser(){
        return userService.selectAll();
    }

    @PostMapping(value = "/addUser")
    public String addUser(User user){
        int save = userService.save(user);
        if(save>0){
            return "新增成功";
        }
        return "新增失败";
    }

    /**
     * 根据用户ID删除用户信息
     * @param id
     * @return
     */
    @DeleteMapping(value = "/deleteUser/{id}")
    public String deleteById(@PathVariable(value = "id")int id){
        int i = userService.deleteById(id);
        if(i>0){
            return "删除成功";
        }
        return "删除失败";
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PostMapping(value = "/updateUser")
    public String updateUser(User user){
        int update = userService.update(user);
        if(update>0){
            return "修改成功";
        }
        return "修改失败";
    }

    /**
     * 测试用
     * @param user
     * @return
     */
    @PostMapping(value = "/OnlyTest")
    public String onlyTest(@RequestBody User user){
        List<String> idArry = user.getIdArry();
        String id = idArry.get(0);
        System.out.println("id"+id);
        return id;
    }
}
